let express = require("express");
let mysql = require("mysql");
let url = require("url");
let form = require("formidable");
let cookie = require("cookie-parser");
const { connect } = require("http2");
const { clear } = require("console");
let app = express();
let connection = mysql.createConnection({
  host:"localhost",
  user:"root",
  password:"root",
  database:"h5-67"
})
connection.connect();
let server = require("http").Server(app);
let io = require("socket.io")(server);
app.use(cookie());
app.use(express.static("node_modules"));
app.set("view engine","ejs");
app.set("views","./views");
//显示主页面
// let denglujieshu = "";
app.get("/",(req,res)=>{
  let user = req.cookies.user;
  let phone = req.cookies.phone;
  let nicheng = req.cookies.nicheng;
  // if(denglujieshu == user){
    // res.clearCookie("user");
  // }
  console.log(user);
  if(user == undefined){
    res.render("lts",{user:user,phone:phone,nicheng:nicheng});
  //
  res.end();
  }else{
     
      res.render("lts",{user:user,phone:phone,nicheng:nicheng});
      res.end();
  }
})

//显示登录页
app.get("/login",(req,res)=>{
  res.render("login");
  res.end();
})
//登录验证

app.post("/dologin",(req,res)=>{
  let form2 = new form.IncomingForm();
  form2.parse(req,(err,fields)=>{
    // console.log(fields);
    let sql = "select * from users where user = ?";
    let arr = [fields.user];
    let sql2 = mysql.format(sql,arr);
    if(fields.yanzhengma1 == fields.yanzhengma2){
      connection.query(sql2,(error,results)=>{
        // console.log(results);
        if(results.length>0){
          if(fields.pass == results[0].pass){
            res.cookie("user",results[0].user,{maxAge:9000000,httpOnly:true});
            res.cookie("phone",results[0].phone,{maxAge:9000000,httpOnly:true});
            res.cookie("nicheng",results[0].nicheng,{maxAge:9000000,httpOnly:true});
  
            // let user1 = req.cookies.user;
            // let phone = req.cookies.phone;
            
            // console.log(user1,phone);
            
           
            
            res.redirect("/");
            res.end();
          }else{
            console.log("密码不正确");
            res.redirect("/login");
            res.end();
          }
        }else{
          console.log("账号不存在");
        res.redirect("/login");
        res.end();
      }
      })
    }else{
      console.log("验证码不正确,请重新输入");
      res.redirect("/login");
      res.end();
    }
    
  })
})
//退出登录
app.get("/tuichulogin",(req,res)=>{
  res.clearCookie("user");
  res.clearCookie("phone");
  res.clearCookie("nicheng");
  res.redirect("/")
  res.end();
})
server.listen(8090);
io.on('connection',socket=>{
  socket.on("h567",data=>{
    console.log(data);
    let {touxiang,datav,nicheng} = data;
    // console.log(touxiang,datav,nicheng);
    let sql = "insert into jilu (touxiang,datav,nicheng) values(?,?,?)";
    let arr = [touxiang,datav,nicheng];
    let sql2 = mysql.format(sql,arr);
    connection.query(sql2,(error,results)=>{
      // console.log(results);
    })
    socket.broadcast.emit('h567',{data});
  })
  
})

io.on('connection',socket=>{
  socket.on("h5-67",function(data){
    socket.broadcast.emit('h5-67',{users:data.user});
  })
});

app.get("/jilu",(req,res)=>{
  // let id = url.parse(req.url,true).query.id;
  // console.log(id);
  let sql = "select * from jilu";
  connection.query(sql,(error,results)=>{
    // console.log(results);
    io.on('connection',socket=>{
      socket.emit("h567jilu",{jilu:results});
      
    });
    res.json({code:200});
    
  })
})


// io.on('connection',socket=>{

  // socket.on("h568",data=>{
    // console.log(data.loginjieshu);

    // denglujieshu = data.loginjieshu;
      // clearCookie("user");
    
  // })
// })


